import java.util.Arrays;

public class Test {
    //二维数组是一个特殊的一维数组
    public static void main(String[] args) {
        int[][] array = new int[][]{{1,2,3},{4,5,6}};
    }









    //二维数组
    public static void main11(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};
        System.out.println(array[0]);//地址
        System.out.println(array[1]);//地址
        System.out.println(Arrays.toString(array));//打印指向一维数组的地址
        System.out.println(Arrays.deepToString(array));//打印二维数组的元素
        System.out.println(array.length);//二维数组的长度
        System.out.println(array[1].length);//一维数组的长度
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }

    }










    //拷贝数组
    public static void main10(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] array1 = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            array1[i] = array[i];
        }
        System.out.println(Arrays.toString(array1));
        int[] a = Arrays.copyOf(array,array.length);//拷贝数组，长度也可以控制
        int[] b = Arrays.copyOf(array,array.length*2);//拷贝数组，也可以扩容
        int[] c = Arrays.copyOfRange(array,1,3);//拷贝数组的特定范围
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
        System.out.println(Arrays.toString(c));
    }









    //给定一个数组，判断有没有三个连续奇数元素，有返回true，没有返回false
    private static boolean fuc8(int[] array){
        int count = 0;
        for (int i = 0; i < array.length; i++) {//遍历
                if (array[i]%2 == 1){
                    count++;
                    if (count==3){
                        return true;//结束
                    }
                }else {
                    count = 0;//重新计数
                }
            }
            return false;
    }
    public static void main9(String[] args) {
        int[] c = {1,2,3,4,5,8,7,9};
        System.out.println(fuc8(c));
    }








    //给定一个数组，找出其多数元素，且该元素个数至少有数组长度的一半以上长，假定该数组总是存在一个多数元素
    public static int fuc7(int[] array){
        Arrays.sort(array);//排序
        return array[array.length/2];
    }
    public static void main8(String[] args) {
        int[] b ={2,2,4,4,4,4,5,5,5,5,5,5};
        System.out.println(fuc7(b));
    }










    //给定一个数组，找出其中单独存在的元素，且其他元素只出现了2次
    public static int fuc5(int[] array){
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret ^= array[i];//0异或任何数字为那个数字，两数相同异或为0
        }
        return ret;
    }
    public static void main7(String[] args) {
        int[] a = {2,1,1,2,3,3,5,7,8,7,8,5,10};
        int b = fuc5(a);
        System.out.println(b);
    }










    //给定一个数组和一个目标值，然后找出数组的两个元素相加为目标值的数组元素的下标，且只找到一组即可
    public static void fuc4(int[] array,int target){
        for (int i = 0; i < array.length-1; i++) {//遍历j和i
            for (int j = i+1; j <array.length ; j++) {
                if (array[i]+array[j]==target){
                    System.out.println(i+" "+j);
                    return;//return是彻底结束方法，break是结束这次的循环
                }
            }
        }
        System.out.println(0);//没有和为目标值的两个数组元素
    }
    public static void main6(String[] args) {
        int[] a = {1,2,3,4,5,6,8};
        fuc4(a,10);
    }










    //二分查找有序数列中指定元素的下标
    public static int binarySort(int[] array,int target){
        int i = 0;
        int j = array.length-1;
        while (i<=j){//注意临界条件的判断，如果相等也是成立
            int mid = (i+j)/2;
            if (array[mid]>target){
                j = mid-1;
            }else if (array[mid] == target){
                return mid;
            }else {
                i = mid+1;
            }
        }
        return -1;
    }
    public static void main5(String[] args) {
        int[] ret ={2,5,6,9,10,12,38};
        System.out.println(binarySort(ret, 10));
        System.out.println(binarySort(ret, 14));
        System.out.println(binarySort(ret, 12));
    }





    //奇数在偶数之前，顺序无要求
    public static void fuc3(int[] array){
        int i = 0;
        int j = array.length-1;
        while (i<j){
            while (i<j && array[i]%2 == 1){
                i++;//可能使i的值大于j
            }
            while (i<j && array[j]%2 == 0){
                j--;
            }
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }
    public static void main4(String[] args) {
        int[] array = {1,6,2,9,7};
        fuc3(array);
        System.out.println(Arrays.toString(array));

    }






    //给数组的每一个元素值扩大
    public static void fuc1(int[] array){//改变实参
        for (int i = 0; i < array.length; i++) {//遍历每一个下标
            array[i] = 2*array[i];
        }
    }
    public static int[] fuc2(int[] array){//未改变实参
        int[] ret = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ret[i] = 2*array[i];
        }
        return ret;
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3};
        fuc1(array);
        System.out.println(Arrays.toString(fuc2(array)));
        System.out.println(Arrays.toString(array));
    }












    //汉诺塔递归实现
    public static void move(char pos1,char pos2){
        System.out.print(pos1+"=>"+pos2+" ");
    }
    public static void hanio(int n,char pos1,char pos2,char pos3){
        if (n == 1){
            move(pos1,pos3);
            return;
        }
        hanio(n-1,pos1,pos3,pos2);//递归循环体1
        move(pos1,pos3);//2
        hanio(n-1,pos2,pos1,pos3);//3
    }
    public static void main1(String[] args) {
        hanio(1,'A','B','C');
        System.out.println();
        hanio(2,'A','B','C');
        System.out.println();
        hanio(3,'A','B','C');

    }
}
